package com.fanyang.study.controller;

import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatOptions;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@RestController
@RequestMapping("/client")
public class DashScopeChatClientController {
    private final ChatClient dashScopeChatClient;
    private static final String DEFAULT_PROMPT = "你好，介绍下你自己！";

    //
//    public DashScopeChatClientController(ChatModel chatModel) {
//
//        // 构造时，可以设置 ChatClient 的参数
//        // {@link org.springframework.ai.chat.client.ChatClient};
//        this.dashScopeChatClient = ChatClient.builder(chatModel)
//                // 实现 Logger 的 Advisor
//                .defaultAdvisors(
//                        new SimpleLoggerAdvisor()
//                )
//                // 设置 ChatClient 中 ChatModel 的 Options 参数
//                .defaultOptions(
//                        DashScopeChatOptions.builder()
//                                .withTopP(0.7)
//                                .build()
//                )
//                .build();
//    }
// 也可以使用如下的方式注入 ChatClient
     public DashScopeChatClientController(ChatClient.Builder chatClientBuilder) {
      	this.dashScopeChatClient = chatClientBuilder.build();
     }

    /**
     * ChatClient 简单调用
     */
    @GetMapping("/simple/chat")
    public String simpleChat() {
        return dashScopeChatClient.prompt(DEFAULT_PROMPT).call().content();
    }

    /**
     * ChatClient 流式调用
     */
    @GetMapping("/stream/chat")
    public Flux<String> streamChat(HttpServletResponse response) {

        response.setCharacterEncoding("UTF-8");
        return dashScopeChatClient.prompt(DEFAULT_PROMPT).stream().content();
    }


}
